<template>
  <div>
    <SearchContainer @search="getData" @reset="() => reset()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="" class="ml-2">
          <a-input v-model:value="formState.zjm" class="w-72" placeholder="姓名/助记码/卡号/手机号/身份证号" allow-clear @keydown.enter="getData" />
        </a-form-item>

        <a-form-item name="dateRange" label="入院日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="onChangeRangePicker" />
        </a-form-item>
        <a-form-item name="yskztCode" label="费用状态">
          <a-select v-model:value="formState.yskztCode" style="width: 260px" placeholder="请选择" allow-clear>
            <a-select-option value="0">正常</a-select-option>
            <a-select-option value="1">催款</a-select-option>
            <a-select-option value="2">欠费</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <BaseTable fill :data-source="tableData" :loading="loading" :scroll="{ x: 1900 }" column-code="000381" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'statusStr'">
            <!-- status  0：在院 1：已出院 2：待出院 3：待入区-->
            <a-tag v-if="record.status === 0" color="blue">在院</a-tag>
            <a-tag v-else-if="record.status === 1" color="green">已出院</a-tag>
            <a-tag v-else-if="record.status === 2" color="orange">待出院</a-tag>
            <a-tag v-else-if="record.status === 3" color="purple">待入区</a-tag>
            <span v-else>——</span>
          </template>
          <!-- yskztCode ("预收款状态 0：正常1:催款 2:欠费") -->
          <!-- status 0：在院 1：已出院 2：待出院 3：待入区 -->
          <template v-if="column.dataIndex === 'action'">
            <!-- v-if="record.yskztCode === 0 && record.status !== 3" -->
            <a-button v-if="record.status !== 3" type="link" size="small" @click="onPrintInventory(record)">住院清单</a-button>
            <!-- v-if="record.yskztCode === 0 && record.status !== 3" -->
            <a-button v-if="record.status !== 3" type="link" size="small" @click="onPrintEveryDoc(record)">每日清单</a-button>
            <a-button
              v-if="record.status === 0 || record.status === 2 || record.status === 3"
              type="link"
              danger
              size="small"
              @click="onOpenReturnModal(record)"
              >退还预收款</a-button
            >
            <TableMoreButton>
              <!-- v-if="record.yskztCode === 1 || record.yskztCode === 2" -->
              <a-button
                v-if="(record.yskztCode === 1 || record.yskztCode === 2) && record.status !== 3"
                type="link"
                size="small"
                danger
                @click="openPayModal(record)"
                >催款单</a-button
              >
              <!-- <a-button v-if="record.status !== 3" type="link" size="small" @click="onPrintMedical(record)">医保报销单</a-button> -->
              <span v-if="record.yskztCode === 0 && record.status === 3">-</span>
            </TableMoreButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <!-- 催款单 -->
    <DemandModal
      v-model:open="depositModelOpen"
      :patient-info="{ ...patientInfo, deJeCount: patientInfo.ljysk, settlementAmount: patientInfo.zyfy, wardName: patientInfo.bqmc }"
      @success="getData"
    />
    <!-- 返还预收款 -->
    <returnChargeModal
      v-model:open="returnChargeModel"
      :is-show-in-title="true"
      :charge-title="returnChargeInfo.chargeTitle"
      :register-price="returnChargeInfo.ljysk"
      :deposit-type="1"
      @success="onReturnSuccessPrice"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import DemandModal from '@/views/in-registration/components/DemandModal.vue'
import returnChargeModal from '@/components/ChargeModal/return'

const formRef = ref(null)
const formState = ref({
  stdate: dayjs(new Date().setDate(new Date().getDate() - 7)).format('YYYY-MM-DD'),
  eddate: dayjs().format('YYYY-MM-DD'),
  zjm: '',
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 7)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const depositModelOpen = ref(false)
const patientInfo = ref({})
const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/inpComprehensive/getExpenseSearch',
  immediate: true,
  params: formState.value,
  formatParams: () => {
    return {
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      zjm: formState.value.zjm,
      yskztCode: formState.value.yskztCode
    }
  }
})

const onPrintInventory = async (row) => {
  const { code, msg, data } = await post('/inp/Print/printAdmissionList', {
    inplsh: row?.inplsh,
    jslb: 'SF',
    dylb: 'ZY'
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const openPayModal = async (row) => {
  patientInfo.value = row
  depositModelOpen.value = true
}
const onPrintEveryDoc = async (row) => {
  const { code, msg, data } = await post('/inp/Print/printAdmissionList', {
    inplsh: row?.inplsh,
    jslb: 'SF',
    dylb: 'MR'
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

// const onPrintMedical = async (row) => {
//   const { code, msg, data } = await post('/inp/Print/printFeeStatements', { inplsh: row?.inplsh })
//   if (code !== 0) return message.error(msg)
//   window.openExe?.rpt(data) //调用winform打印
// }
const returnChargeModel = ref(false)
const returnChargeInfo = ref({})
const onOpenReturnModal = async (record) => {
  returnChargeInfo.value = record
  const res = await post('/inp/inpComprehensive/getBillAmount', {
    inplsh: record.inplsh
  })
  if (res.code !== 0) {
    message.error(res.msg || '操作失败')
    return
  } else {
    returnChargeInfo.value.chargeTitle = res.data.memo
    returnChargeModel.value = true
  }
}
const onReturnSuccessPrice = async (params) => {
  const res = await post('/inp/inpComprehensive/inpRefundDeposit', {
    typeOperation: '2',
    inplsh: returnChargeInfo.value.inplsh,
    ...params
  })
  if (res.code !== 0) {
    message.error(res.msg || '操作失败')
    return
  } else {
    message.success(res.msg)
    returnChargeModel.value = false
    getData()
  }
}

const onChangeRangePicker = (e) => {
  formState.value.stdate = e[0]
  formState.value.eddate = e[1]
}
const reset = () => {
  formRef.value.resetFields()
  getData()
}
</script>
